//
//  personRoleView.m
//  Estate
//
//  Created by kevin on 16/9/13.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "personRoleView.h"
#import "SelectionCell.h"
#import "UITableView+DataSourceBlocks.h"
#import "TableViewWithBlock.h"
#import "userModel.h"
#import "CommonFunc.h"
#import "dataInit.h"

@implementation personRoleView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
       
        [self setUpUI];
        
    }
    return self;
}

//初始化UI
-(void)setUpUI{
    
    //视图框
    UIView *subView=[[UIView alloc]initWithFrame:CGRECTS(375 * 0.1, 667 *0.25, 375 * 0.8,667 *0.5)];
    subView.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    [self addSubview:subView];
    
    self.closeButton = [[UIButton alloc]init];
    self.closeButton.layer.cornerRadius = 10;
    [self.closeButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    UIImage *closeImage=[UIImage imageNamed:@"Close"];
    [self.closeButton setImage:closeImage forState:UIControlStateNormal];
    [self addSubview:self.closeButton];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(subView.mas_right).mas_offset(-15);
        make.top.mas_equalTo(subView.mas_top).mas_offset(-15);
        make.width.offset(30);
        make.height.offset(30);
    }];
    
    self.userName = [[UILabel alloc]init];
    self.userName.font = [UIFont systemFontOfSize:13*BIZ];
    self.userName.backgroundColor = [UIColor whiteColor];
    self.userName.frame = CGRECTS(375 * 0.1, 40, 375 * 0.6, 30);
    self.userName.layer.cornerRadius = 3;
    [subView addSubview:self.userName];
    
    UIButton *btn = [[UIButton alloc]init];
    UIImage *image = [UIImage imageNamed:@"Icon"];
    [btn setImage:image forState:UIControlStateNormal];
    btn.enabled = NO;
    [subView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.userName.mas_centerY);
        make.right.mas_equalTo(self.userName.mas_right).mas_offset(-15);
        make.width.offset(20);
        make.height.offset(20);
    }];

    self.roleC = [[UILabel alloc]init];
    self.roleC.text = @"   请选择角色";
    self.roleC.font = [UIFont systemFontOfSize:13*BIZ];
    self.roleC.frame = CGRECTS(375 * 0.1, self.userName.frame.size.height + self.userName.frame.origin.y + 15, 375 * 0.6, 30);
    self.roleC.backgroundColor = [UIColor whiteColor];
    [self.roleC.layer setBorderColor:[TTTheme defaultManger].selectColor.CGColor];
    [self.roleC.layer setBorderWidth:2];
    self.roleC.layer.cornerRadius = 3;
    [subView addSubview:self.roleC];
    
    self.openButton = [[UIButton alloc]init];
    UIImage *openImage=[UIImage imageNamed:@"Menu1"];
    [self.openButton setImage:openImage forState:UIControlStateNormal];
    [self.openButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [subView addSubview:self.openButton];
    [self.openButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.roleC.mas_centerY);
        make.right.mas_equalTo(self.roleC.mas_right).mas_offset(-15);
        make.width.offset(20);
        make.height.offset(20);
    }];
    
    _tb = [[TableViewWithBlock alloc]init];
    _tb.frame = CGRECTS(375 * 0.1, self.roleC.frame.size.height + self.roleC.frame.origin.y - 2,  375 * 0.6, 0);
    _tb.bounces = NO;
    _tb.showsVerticalScrollIndicator = NO;
    _tb.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tb.rowHeight=30*BIZ;
    [_tb registerClass:[SelectionCell class] forCellReuseIdentifier:@"SelectionCell"];
    
    [_tb initTableViewDataSourceAndDelegate:^NSInteger(UITableView *tableView, NSInteger section) {
        return self.dataArray.count;
        
    } setCellForIndexPathBlock:^(UITableView *tableView,NSIndexPath *indexPath){
        SelectionCell *cell=[tableView dequeueReusableCellWithIdentifier:@"SelectionCell"];
        NSDictionary *dic=self.dataArray[indexPath.row];
        [cell setDic:dic];
        cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame];
        
        cell.selectedBackgroundView.backgroundColor =[TTTheme defaultManger].selectColor;

        return cell;
    } setDidSelectRowBlock:^(UITableView *tableView,NSIndexPath *indexPath){
        SelectionCell *cell=(SelectionCell*)[tableView cellForRowAtIndexPath:indexPath];
        NSDictionary *dic=self.dataArray[indexPath.row];
        self.selectedData=dic;
        //设置显示框
        self.roleC.text=cell.lb.text;
        [_openButton sendActionsForControlEvents:UIControlEventTouchUpInside];
        
        cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame];
        
        cell.selectedBackgroundView.backgroundColor =[TTTheme defaultManger].selectColor;
        
    }];
    [self.tb.layer setBorderColor:[TTTheme defaultManger].selectColor.CGColor];
    [_tb.layer setBorderWidth:2];
    [subView addSubview:self.tb];
    
    self.cerTainButton = [[UIButton alloc]init];
    self.cerTainButton.backgroundColor = [TTTheme defaultManger].selectColor;
    [self.cerTainButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.cerTainButton.frame = CGRECTS(self.userName.frame.origin.x, 200, 100, 30);
    [self.cerTainButton setTitle:@"确   定" forState:UIControlStateNormal];
    [self.cerTainButton addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    [subView addSubview:self.cerTainButton];
    
    self.canCelButton = [[UIButton alloc]init];
    self.canCelButton.backgroundColor = [UIColor grayColor];
    self.canCelButton.frame = CGRECTS(self.cerTainButton.frame.origin.x + self.cerTainButton.frame.size.width + 20, 200, 100, 30);
    [self.canCelButton setTitle:@"取   消" forState:UIControlStateNormal];
    [self.canCelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [self.canCelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [subView addSubview:self.canCelButton];

}


//点击确定取消按钮
-(void)sure{
    if(self.sureBlock){
        self.sureBlock(self.selectedData);
    }
}

- (void)cancel{
    if(self.cancelBlock){
        self.cancelBlock();
    }
}

//点击选择角色按钮
-(void)click{

    if (isOpened) {
        
        [UIView animateWithDuration:0.3 animations:^{
            UIImage *closeImage=[UIImage imageNamed:@"Menu1"];
            [_openButton setImage:closeImage forState:UIControlStateNormal];
            
            CGRect frame=_tb.frame;
            
            self.cerTainButton.hidden = NO;
            
            self.canCelButton.hidden = NO;

            frame.size.height=0;
            
            [_tb setFrame:frame];
            
            
        } completion:^(BOOL finished){
            
            isOpened=NO;
        }];
    }else{
        
        [UIView animateWithDuration:0.3 animations:^{
            UIImage *openImage=[UIImage imageNamed:@"Menu"];
            [_openButton setImage:openImage forState:UIControlStateNormal];
            
            CGRect frame=_tb.frame;
            
            self.cerTainButton.hidden = YES;
            
            self.canCelButton.hidden = YES;

            frame.size.height=200;
            [_tb setFrame:frame];
        } completion:^(BOOL finished){
            
            isOpened=YES;
            
        }];
        
    }
    
}

@end
